package com.tenqian.crm.controller;

import com.tenqian.Result;
import com.tenqian.crm.domain.TqUser;
import com.tenqian.crm.service.TqUserService;
import com.tenqian.enums.ResultEnum;
import com.tenqian.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class TqUserController {
    Logger logger = LoggerFactory.getLogger(TqUserController.class);

    @Autowired
    TqUserService tqUserService;

    @RequestMapping("/regist")
    @ResponseBody
    public Result regist(TqUser tqUser) {
        try {
            //md5加密
            tqUser.setPassword(DigestUtils.md5DigestAsHex(tqUser.getPassword().getBytes()));
            return tqUserService.save(tqUser);
        } catch (Exception e) {
            logger.error("注册失败:{}", e.getMessage());
            return ResultUtil.error(ResultEnum.REGIST_SERVER_ERROR);
        }
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(TqUser tqUser) {
        if (StringUtils.isBlank(tqUser.getPassword())) return ResultUtil.error(ResultEnum.LOGIN_ERROR);
        try {
            //md5加密
            tqUser.setPassword(DigestUtils.md5DigestAsHex(tqUser.getPassword().getBytes()));
            return tqUserService.login(tqUser);
        } catch (Exception e) {
            logger.error("登录失败:", e);
            return ResultUtil.error(ResultEnum.LOGIN_SERVER_ERROR);
        }
    }

    @RequestMapping("/deltoken")
    @ResponseBody
    public Result delToken(String token) {
        try {
            return tqUserService.delToken(token);
        } catch (Exception e) {
            logger.error("删除token失败:{}", e.getMessage());
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    @RequestMapping("/updateHeadImg")
    @ResponseBody
    public Result updateHeadImg(@RequestParam("headImg") String headImg, @RequestParam("token") String token) {
        try {
            return tqUserService.updateHeadImg(token, headImg);
        } catch (Exception e) {
            logger.error("上传头像失败:{}", e.getMessage());
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/addFriend")
    @ResponseBody
    public Result addFriend(@RequestParam("id") String id, @RequestParam("friendId") String friendId) {
        try {
            return tqUserService.addFriend(id, friendId);
        } catch (Exception e) {
            logger.error("添加好友失败:{}", e.getMessage());
            return ResultUtil.error(ResultEnum.ADD_FRIEND_ERROR);
        }
    }

    @RequestMapping("/findByNickNameOrTelephone")
    @ResponseBody
    public Result findByNickNameOrTelephone(@RequestParam("q") String q) {
        try {
            return tqUserService.findByNickNameOrTelephone(q);
        } catch (Exception e) {
            logger.error("查询失败:{}", e.getMessage());
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        }
    }

    @RequestMapping("/findFriendsByToken")
    @ResponseBody
    public Result findFriendsByToken(@RequestParam("token") String token) {
        try {
            return tqUserService.findFriendsByToken(token);
        } catch (Exception e) {
            logger.error("查询失败:", e);
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/findUserById")
    @ResponseBody
    public Result findUserById(@RequestParam("id") String id) {
        try {
            return tqUserService.findUserById(id);
        } catch (Exception e) {
            logger.error("查询失败:", e);
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/findUserByToken")
    @ResponseBody
    public Result findUserByToken(@RequestParam("token") String token) {
        try {
            return ResultUtil.success(tqUserService.findUserByToken(token));
        } catch (Exception e) {
            logger.error("查询失败:", e);
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/delFriend")
    @ResponseBody
    public Result delFriend(@RequestParam("id") String id, @RequestParam("friendId") String friendId) {
        try {
            return tqUserService.delFriend(id, friendId);
        } catch (Exception e) {
            logger.error("删除好友失败:{}", e.getMessage());
            return ResultUtil.error(e);
        }
    }

    @RequestMapping("/sendCode")
    @ResponseBody
    public Result sendCode(@RequestParam("telephone") String telephone) {
        try {
            return tqUserService.sendCode(telephone);
        } catch (Exception e) {
            logger.error("发送验证码失败:{}", e.getMessage());
            return ResultUtil.error(e);
        }
    }
}
